package vclass
{
import mx.managers.CursorManager;
import mx.rpc.events.InvokeEvent;
import mx.events.CloseEvent;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
import mx.core.Application;
public class Vhttp extends HTTPService
    {
    	private var myXML:String;
        public var target:Object;
        public var withConfirmare:Boolean = false;
        
    	public function Vhttp(){
    		super();
    		this.method = "POST";
    		this.resultFormat = "e4x";
    		this.addEventListener(InvokeEvent.INVOKE,bsInvokeHandler);
			this.addEventListener(ResultEvent.RESULT,bsResultHandler);
			this.addEventListener(FaultEvent.FAULT,bsFaultHandler);
    	}
    	private function bsInvokeHandler(event:InvokeEvent):void
					{
					// Display the busy cursor
					CursorManager.setBusyCursor();
					this.removeEventListener(InvokeEvent.INVOKE,bsInvokeHandler);
					}
		private function bsResultHandler(event:ResultEvent):void
					{
					if(target != null){
						target.dataProvider = event.result..row;
					}
					//check for errors
					var messid:Number = event.result.attribute("messid");
					var myrpc:String = event.result.attribute("rpc");
					if(event.result.attribute("flag") == "0"){
						messAlert(messid,myrpc);
					}else{
						if(withConfirmare){
							confirmAlert(messid);
						}
					}
					//prepare output
					myXML = String(event.result);
					// Hide the busy cursor
					CursorManager.removeBusyCursor();
					this.removeEventListener(ResultEvent.RESULT,bsResultHandler);
					this.removeEventListener(FaultEvent.FAULT,bsResultHandler);
					}
		private function bsFaultHandler(event:FaultEvent):void
					{
					// There was an error in loading the XML
					Alert.show (event.fault.message); 
					// Hide the busy cursor
					CursorManager.removeBusyCursor();
					//remove listeners
					this.removeEventListener(InvokeEvent.INVOKE,bsInvokeHandler);
					this.removeEventListener(ResultEvent.RESULT,bsResultHandler);
					this.removeEventListener(FaultEvent.FAULT,bsResultHandler);
					}
		public function get getXML():String
		{
			return myXML;
		}
		//errors handle
		private function confirmAlert(messid:Number):void{
			var mesaj:String = Application.application.messages.row.(@id==messid).@continut;
			Alert.show(mesaj);
		}
		private function messAlert(messid:Number, rpc:String):void{
			Application.application.alertErrorInvoked = true;
			var mesaj:String = Application.application.messages.row.(@id==messid).@continut+" RPC:"+rpc;
			function alertOkHandler(event:CloseEvent):void{
				Application.application.alertErrorInvoked = false;
				Application.application.currentState = 'login';
			}
			if(messid==6){
				//asta e cazul de looged out
				Alert.show(mesaj,"Error",4,null,alertOkHandler);
				return;
			}else{
				//alte erori
				Alert.show(mesaj,"Error");
				return;
			}
		}
		
    }
}